package com.freesky.web.manager.service.impl;

import com.freesky.common.enums.RedisKeyEnum;
import com.freesky.common.enums.ResponseEnum;
import com.freesky.common.pojo.UserLoginInfo;
import com.freesky.common.redis.IRedisTemplate;
import com.freesky.common.utils.TimeUtil;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.web.manager.service.ILoginService;
import com.freesky.web.manager.shiro.CurrentUser;
import com.freesky.web.manager.util.SystemUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 9:24 2019-01-11
 * @description:
 */
@Service
@Transactional
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private IRedisTemplate redisTemplate;

    @Override
    public JSONResponse validateLogin(String username, String password, String verifyCode, HttpServletRequest request) {
        String sessionVerifyCode = (String) request.getSession().getAttribute("verifyCode");
        // 验证码输入不正确
        if (!sessionVerifyCode.equalsIgnoreCase(verifyCode)) {
            return JSONResponseDiretor.buildSuccessJSONResponse(ResponseEnum.LOGIN_ERROR_VERIFY_CODE, null);
        }
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        usernamePasswordToken.setRememberMe(true);
        Subject subject = SecurityUtils.getSubject();
        //完成登录
        subject.login(usernamePasswordToken);
        //设置登录信息
        UserLoginInfo userLoginInfo= redisTemplate.getCacheObject(RedisKeyEnum.USER_LOGIN_INFO_PREFIX.getKey()+"-"+username, UserLoginInfo.class);
        //第一次登录userLoginInfo为空
        //本地登录时间
        String thisLoginTime = TimeUtil.getDateTimeNow();
        //本次登录IP
        String thisLoginIpAddr = SystemUtil.getIpAddr(request);
        //本地登录IP
        if (userLoginInfo==null) {
            userLoginInfo = new UserLoginInfo();
            userLoginInfo.setLoginTimes(1);
        }else{
            userLoginInfo.setLoginTimes(userLoginInfo.getLoginTimes()+1);
        }
        userLoginInfo.setThisLoginTime(thisLoginTime);
        userLoginInfo.setThisLoginIpAddr(thisLoginIpAddr);
        redisTemplate.putCache(RedisKeyEnum.USER_LOGIN_INFO_PREFIX.getKey()+"-"+username,userLoginInfo);
        return JSONResponseDiretor.buildSuccessJSONResponse(ResponseEnum.LOGIN_SUCCESS, null);
    }

    @Override
    public String logout() {
        String username = CurrentUser.INSTANCE.getUser().getUsername();
        //设置登录信息
        UserLoginInfo userLoginInfo= redisTemplate.getCacheObject(RedisKeyEnum.USER_LOGIN_INFO_PREFIX.getKey()+"-"+username, UserLoginInfo.class);
        if (userLoginInfo != null) {
            userLoginInfo.setLastLoginTime(userLoginInfo.getThisLoginTime());
            userLoginInfo.setLastLoginIpAddr(userLoginInfo.getThisLoginIpAddr());
            redisTemplate.putCache(RedisKeyEnum.USER_LOGIN_INFO_PREFIX.getKey()+"-"+username,userLoginInfo);
        }
        SecurityUtils.getSubject().logout();
        return "login";
    }
}
